//
//  ButtonViewController.m
//  test1
//
//  Created by chen on 16/11/5.
//  Copyright © 2016年 chen. All rights reserved.
//

#import "ButtonViewController.h"
#import "RecommendsModel.h"
#import "BottomTableViewCell.h"

@interface ButtonViewController () <UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSData *data;
@property(nonatomic,strong)NSArray *arrMDiscover;
@end

@implementation ButtonViewController

-(instancetype)initWithUrl :(NSURL *)url {
    if (self = [super init]) {
        self.url = url;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    
    //设置tableView
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BottomTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell5"];
    
}

- (void)loadData {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        _data = [NSData dataWithContentsOfURL:_url];
        if (_data ==nil) {
//            NSLog(@"data为空"); //时间间隔是60s.
            return ;
        }
//        NSLog(@"data不为空");
        //获得json中的词典
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_data options:0 error:nil];
        //获得字典中key为result 的数组
        NSArray *arrResult = dict[@"result"];
        
        NSMutableArray *arrMDiscover = [NSMutableArray array];
        for (NSDictionary *dict in arrResult) {
            RecommendsModel *model = [RecommendsModel recommendsModelWithDict:dict];
            [arrMDiscover addObject:model];
        }
        self.arrMDiscover = arrMDiscover.copy;
        
        dispatch_async( dispatch_get_main_queue() , ^{
            [self.tableView reloadData];
//            [self loadViewIfNeeded];
        });
    });
}

-(void)setUrl:(NSURL *)url {
    
    _url = url;
    
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    if (self.tableView.contentOffset.y < 0) {
        
        [self loadData];
    }
    
}

#pragma mark ---  UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrMDiscover.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BottomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell5" forIndexPath:indexPath];
    
    cell.model = self.arrMDiscover[indexPath.row];
    
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
